package com.ocm.tourism.core.dao.jpa;

import java.util.List;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import com.ocm.tourism.core.dao.CommentDao;
import com.ocm.tourism.core.domain.Comment;

@Repository("commentDao")
public class JpaCommentDao extends JpaGenericDao<Comment> implements CommentDao {

   public JpaCommentDao() {
      this.setClazz(Comment.class);
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<Comment> getAttractionComments(String attractionType, int firstResult, int sizeNo) {
      Query query = this.entityManager()
            .createQuery("SELECT c FROM Comment c WHERE c.attractionType =:attractionType ORDER BY c.createdDate DESC", Comment.class)
            .setFirstResult(firstResult).setMaxResults(sizeNo);
      query.setParameter("attractionType", attractionType);
      List<Comment> comments = query.getResultList();
      return comments;
   }

   @Override
   public long count(String attractionType) {
      Query query = this.entityManager().createQuery(
            "SELECT COUNT(c) FROM Comment c WHERE c.attractionType =:attractionType", Long.class);
      query.setParameter("attractionType", attractionType);
      long count = (Long) query.getSingleResult();
      return count;
   }

   @Override
   public long countByCatalogue(long catalogueId) {
      Query query = this.entityManager().createQuery(
            "SELECT COUNT(c) FROM Comment c WHERE c.catalogueId =:catalogueId", Long.class);
      query.setParameter("catalogueId", catalogueId);
      long count = (Long) query.getSingleResult();
      return count;
   }
   
   @Override
   public long countByBusiness(long businessId) {
      Query query = this.entityManager().createQuery(
            "SELECT COUNT(c) FROM Comment c WHERE c.businessId =:businessId", Long.class);
      query.setParameter("businessId", businessId);
      long count = (Long) query.getSingleResult();
      return count;
   }
   
   @SuppressWarnings("unchecked")
   @Override
   public List<Comment> getCommentsByCatalogue(long catalogueId, int firstResult, int sizeNo) {
      Query query = this.entityManager()
            .createQuery("SELECT c FROM Comment c WHERE c.catalogueId =:catalogueId ORDER BY c.createdDate DESC", Comment.class)
            .setFirstResult(firstResult).setMaxResults(sizeNo);
      query.setParameter("catalogueId", catalogueId);
      List<Comment> comments = query.getResultList();
      return comments;
   }
   
   @SuppressWarnings("unchecked")
   @Override
   public List<Comment> getCommentsByBusiness(long businessId, int firstResult, int sizeNo) {
      Query query = this.entityManager()
            .createQuery("SELECT c FROM Comment c WHERE c.businessId =:businessId ORDER BY c.createdDate DESC", Comment.class)
            .setFirstResult(firstResult).setMaxResults(sizeNo);
      query.setParameter("businessId", businessId);
      List<Comment> comments = query.getResultList();
      return comments;
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<Comment> getCommentsByTour(long tourId, int firstResult, int sizeNo) {
      Query query = this.entityManager()
            .createQuery("SELECT c FROM Comment c WHERE c.tourId =:tourId ORDER BY c.createdDate DESC", Comment.class)
            .setFirstResult(firstResult).setMaxResults(sizeNo);
      query.setParameter("tourId", tourId);
      List<Comment> comments = query.getResultList();
      return comments;
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<Comment> getCommentsByTourSale(long saleId, int firstResult, int sizeNo) {
      Query query = this.entityManager()
            .createQuery("SELECT c FROM Comment c WHERE c.saleId =:saleId ORDER BY c.createdDate DESC", Comment.class)
            .setFirstResult(firstResult).setMaxResults(sizeNo);
      query.setParameter("saleId", saleId);
      List<Comment> comments = query.getResultList();
      return comments;
   }

   @Override
   public long countByTour(long tourId) {
      Query query = this.entityManager().createQuery(
            "SELECT COUNT(c) FROM Comment c WHERE c.tourId =:tourId", Long.class);
      query.setParameter("tourId", tourId);
      long count = (Long) query.getSingleResult();
      return count;
   }

   @Override
   public long countByTourSale(long saleId) {
      Query query = this.entityManager().createQuery(
            "SELECT COUNT(c) FROM Comment c WHERE c.saleId =:saleId", Long.class);
      query.setParameter("saleId", saleId);
      long count = (Long) query.getSingleResult();
      return count;
   }
}
